home *** CD-ROM | disk | FTP | other *** search
/ Delphi Magazine Collection 2001 / Delphi Magazine Collection 20001 (2001).iso / Bonus / Plasmatech / ptscp_examples.exe / %MAINDIR% / Examples / NewFileEdit / CBuilder / FMain.cpp next >
Encoding:
C/C++ Source or Header  |  2001-08-31  |  3.0 KB  |  83 lines

  1. //---------------------------------------------------------------------------
  2. #include <vcl.h>
  3. #pragma hdrstop
  4.  
  5. #include "FMain.h"
  6. #include "UPTShellUtils.hpp"
  7. //---------------------------------------------------------------------------
  8. #pragma package(smart_init)
  9. #pragma link "UPTShellControls"
  10. #pragma link "UPTTreeList"
  11. #pragma resource "*.dfm"
  12. TfrmMain *frmMain;
  13. //---------------------------------------------------------------------------
  14. __fastcall TfrmMain::TfrmMain(TComponent* Owner)
  15.     : TForm(Owner)
  16. {
  17. }
  18. //---------------------------------------------------------------------------
  19. void __fastcall TfrmMain::MakeNewFile()
  20. {
  21.   static int FileNumber = 0;
  22.   try {
  23.     PTShellList1->Options = PTShellList1->Options >> ptsloDynamicRefresh;
  24.       // We will handle the refreshing ourselves, so turn off automatic mode for now.
  25.  
  26.     FCreatingFile = true;
  27.       // This flag is checked in the OnInsert event handler.
  28.  
  29.     FileNumber++;
  30.       // Give the file a unique number.
  31.  
  32.     FNewFileName = Uptshellutils::EnsureTrailingCharDB(PTShellList1->Folder->PathName,'\\') +
  33.                    "NewFile"+IntToStr(FileNumber);
  34.       // EnsureTrailingCharDB is a double-byte safe function that makes sure the last character
  35.       // in the string is the passed character '\' in this case. Most folders don't end with a '\',
  36.       // but root drive folders (c:\) do. Using this function we can get the path name into a
  37.       // consistent format to which we can append the file name.}
  38.  
  39.     ShowMessage( FNewFileName );
  40.  
  41.     delete( new TFileStream(FNewFileName, fmCreate | fmOpenWrite | fmShareDenyNone) );
  42.       // Create the new file stream, free it straight away. This will create an empty file.
  43.  
  44.     if (IsWin95() &&  PTShellList1->Folder->IdList == NULL)
  45.       Sleep(2000);
  46.       // Under Windows 95 (and variants) the desktop virtual folder is very slow in registering the
  47.       // presence of the new file.
  48.  
  49.     PTShellList1->RefreshItems();
  50.       // Before this returns, the OnInsert item will be called 1..n times.
  51.  
  52.     FCreatingFile = false;
  53.     PTShellList1->Options = PTShellList1->Options << ptsloDynamicRefresh;
  54.   }
  55.   catch(...) {
  56.     FCreatingFile = false;
  57.     PTShellList1->Options = PTShellList1->Options << ptsloDynamicRefresh;
  58.     throw;
  59.   }
  60. }
  61. //---------------------------------------------------------------------------
  62. void __fastcall TfrmMain::PTShellList1Insert(TObject *Sender,
  63.       TListItem *Item)
  64. {
  65.   if (FCreatingFile  &&
  66.     AnsiCompareFileName(PTShellList1->GetDataFromItem(Item)->PathName, FNewFileName)==0 )
  67.   {
  68.     Item->MakeVisible(false);
  69.       // Make the item fully visible.
  70.  
  71.     Item->EditCaption();
  72.  
  73.     FCreatingFile = false;
  74.       // Since we only added one file, we don't need to check any more.
  75.   }
  76. }
  77. //---------------------------------------------------------------------------
  78. void __fastcall TfrmMain::btnCreateClick(TObject *Sender)
  79. {
  80.   MakeNewFile();
  81. }
  82. //---------------------------------------------------------------------------
  83.